<template>
	<view>
		<uni-popup @change="changepo" :mask-click="false" type="center" ref="updata">
			<view class="cu-modal version show">
				<view class="cu-dialog upgrade">
					<view class="des">
						<pre>{{content}}</pre>
					</view>
					<view class="bg-white padding-lr padding-top"
						style="border-radius:0 0 20rpx 20rpx;padding-bottom:1px;">
						<view class="updatebtn" :class="{updatebtn_fail:progress==500}" @tap="doUpData">
							{{progress===500?'下载失败，请重试':progress===0?'立即更新':('正在下载...'+progress+'%')}}</view>
						<view v-if="!forceable" class="dialog_close" @tap="close">×</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				forceable: true,
				version: "",
				content: [],
				androidUrl: "",
				iosUrl: "",
				visible: false,
				progress: 0,
				flat: false,
				appstore: "",
				os_type: "",
				package_type: ""
			}
		},

		mounted() {
			this.getversion()
		},
		methods: {
			changepo(e) {
				if (e.show) {
					uni.hideTabBar()
				} else {
					uni.showTabBar()
				}
			},
			getversion() {
				// let version = uni.getStorageSync('version')?uni.getStorageSync('version'):plus.runtime.version
				var pla = plus.os.name.toLowerCase() == 'android' ? 1 : 2
				this.http('/api/get_new_app/' + pla).then(res => {
					if (res.data) {
						var prevVersion = this.fixZeroEnd(uni.getStorageSync('version'), 6).replace(/\./g, '')
						var nextVersion = this.fixZeroEnd(res.data.version, 6).replace(/\./g, '')
						if (Number(prevVersion) < Number(nextVersion)) {
							//this.package_type = res.data.package_type
							this.androidUrl = res.data.url
							//this.os_type = res.data.os_type
							//this.appstore = res.data.appstore
							this.content = res.data.info
							this.version = res.data.version
							this.$refs.updata.open()
						}
					}
				})
			},
			fixZeroEnd(str, n) {
				return (str + Array(n).join(0)).slice(0, n)
			},
			doUpData() {
				var prevVersion = this.fixZeroEnd(plus.runtime.version, 6).replace(/\./g, '')
				// if (Number(prevVersion) <= 999) {
				// 	if(this.os_type == 'ios'){
				// 		plus.runtime.openURL(this.appstore)
				// 	}else{
				// 		plus.runtime.openURL(this.androidUrl, err=>{
				// 					this.openURL(this.androidUrl)
				// 		},'com.android.browser')
				// 	}
				// 	return
				// }else{

				if (this.androidUrl.endsWith(".wgt")) {

				} else {
					if (this.androidUrl.endsWith(".ipa")) {
						plus.runtime.openURL(this.androidUrl)
					} else {
						plus.runtime.openURL(this.androidUrl, err => {
							this.openURL(this.androidUrl)
						}, 'com.android.browser')
					}
					return
				}

				//}

				if (this.flat) {
					return
				}

				this.flat = true
				// 整包 
				var loadflag = false
				var _this = this
				var osname = plus.os.name

				uni.showLoading({
					mask: true
				})
				// 安卓打开网页下载

				//plus.runtime.openURL(this.downloadUrl_android);
				var downloadTask = uni.downloadFile({ //执行下载
					url: _this.androidUrl, //下载地址
					success: downloadResult => { //下载成功
						if (downloadResult.statusCode === 200) {
							plus.runtime.install(downloadResult.tempFilePath, {
								force: false
							}, function(e) {
								uni.setStorageSync('version', _this.version)
								_this.visible = false
								_this.$refs.updata.close()
								setTimeout(() => {
									plus.runtime.restart();
									uni.hideLoading()
								}, 100)
							}, function(e) {
								uni.showModal({
									content: JSON.stringify(e)
								})
							});
						}
					},
					complete(e) {

					}
				})
				setTimeout(() => {
					if (!loadflag) {
						this.progress = 500
						uni.hideLoading()
						this.flat = false
					}
				}, 10000)
				downloadTask.onProgressUpdate(res => {
					loadflag = true
					if (res && res.progress) {
						this.progress = res.progress
					}
				})

			},
			close() {
				uni.showTabBar()
				this.visible = false
				this.$refs.updata.close()
			}
		}
	}
</script>

<style scoped>
	.cu-modal {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 1110;
		opacity: 0;
		outline: 0;
		text-align: center;
		-ms-transform: scale(1.185);
		transform: scale(1.185);
		backface-visibility: hidden;
		perspective: 2000upx;
		background: rgba(0, 0, 0, 0.6);
		transition: all 0.3s ease-in-out 0s;
		pointer-events: none;
	}

	.cu-modal::before {
		content: "\200B";
		display: inline-block;
		height: 100%;
		vertical-align: middle;
	}

	.cu-modal.show {
		opacity: 1;
		transition-duration: 0.3s;
		-ms-transform: scale(1);
		transform: scale(1);
		overflow-x: hidden;
		overflow-y: auto;
		pointer-events: auto;
	}

	.cu-dialog {
		position: relative;
		display: inline-block;
		vertical-align: middle;
		margin-left: auto;
		margin-right: auto;
		width: 680upx;
		max-width: 100%;
		background-color: #f8f8f8;
		border-radius: 10upx;
		overflow: hidden;
	}

	.cu-modal .cu-dialog>.cu-bar:first-child .action {
		min-width: 100rpx;
		margin-right: 0;
		min-height: 100rpx;
	}

	.upgrade {
		position: relative;
		min-height: 480rpx;
		overflow: visible;
		width: 550rpx;
		padding-top: 320rpx;
		background: url(./upgrade.png) center 0 / 550rpx 504rpx no-repeat;
	}

	.upgrade image {
		position: absolute;
		top: -130rpx;
		right: 0;
		width: 550rpx;
		height: 504rpx;
	}

	.version {
		font-size: 0;
		z-index: 99999;
	}

	.version .padding {
		font-size: 28rpx;
	}

	.version .des {
		text-align: left;
		line-height: 36rpx;
		padding: 20rpx;
		font-size: 28rpx;
		line-height: 48rpx;
		color: #0B0A12;
	}

	.updatebtn {
		background: linear-gradient(90deg, #1A86FF, #00B2FF);
		color: #fff;
		line-height: 80rpx;
		height: 80rpx;
		text-align: center;
		border-radius: 40rpx;
		font-size: 28rpx;
		margin: 0 auto 20rpx;
	}

	.updatebtn_fail {
		background: #EE1600;
	}

	.dialog_close {
		margin-bottom: 20rpx;
		color: #000;
		font-size: 48rpx;
	}

	.bg-white {
		background-color: #fff;
	}

	.padding-lr {
		padding: 0 30rpx;
	}

	.padding-top {
		padding-top: 30rpx;
	}
</style>